package com.wufeng.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wufeng.dto.R;
import com.wufeng.entity.ReplyLikeRecord;
import com.wufeng.service.ReplyLikeRecordService;
import com.wufeng.service.ReplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 *  回复点赞记录(ReplyLikeRecord)表控制层
 */
@RestController
@RequestMapping("replylikerecord")
public class ReplyLikeRecordController {

    @Autowired
    private ReplyLikeRecordService replyLikeRecordService;


    @Autowired
    private ReplyService replyService;


    /**
     * 根据用户id和回复id添加或更新记录
     *
     * @param replyLikeRecord
     * @return
     */
    @Transactional
    @PostMapping("/addOrUpdateReplyLikeRecordByUserIdAndReplyId")
    public R addOrUpdateReplyLikeRecordByUserIdAndReplyId(@RequestBody ReplyLikeRecord replyLikeRecord) {

        ReplyLikeRecord  record = replyLikeRecordService.getOne(
                new QueryWrapper<ReplyLikeRecord>().
                        eq("userId", replyLikeRecord.getUserid()).
                        eq("replyId", replyLikeRecord.getReplyid())
        );
        Map<String, Object> map = new HashMap<>();
        boolean isUpdate = false;
        if (record == null) {//添加
            replyLikeRecord.setIslike(1);//点赞

            isUpdate = replyLikeRecordService.save(replyLikeRecord);
            //更新文章点赞数+1
            replyService.incrementLikeCount(replyLikeRecord.getReplyid());

            map.put("isUpdate", isUpdate);
            return R.ok(map);
        }

        //更新
        if (record.getIslike() == 1) {
            record.setIslike(0);
            //更新文章点赞数-1
            replyService.decrementLikeCount(replyLikeRecord.getReplyid());
        } else {
            record.setIslike(1);
            //更新文章点赞数+1
            replyService.incrementLikeCount(replyLikeRecord.getReplyid());
        }
        isUpdate = replyLikeRecordService.update(record, new QueryWrapper<ReplyLikeRecord>().
                eq("userId", replyLikeRecord.getUserid()).
                eq("replyId", replyLikeRecord.getReplyid())
        );
        map.put("isUpdate", isUpdate);
        return R.ok(map);
    }


    /**
     * 根据回复id获取该回复的点赞数
     *
     * @param replyId
     * @return
     */
    @GetMapping("/getLikeCountByReplyId")
    public R getLikeCountByReplyId(@RequestParam("replyId") Integer replyId) {
        long count = replyLikeRecordService.count(new QueryWrapper<ReplyLikeRecord>()
                .eq("replyId", replyId)
                .eq("isLike", 1));
        HashMap<String, Object> map = new HashMap<>();
        map.put("likeCount", count);
        return R.ok(map);
    }


    /**
     * 根据回复id和用户id判断是否点赞
     * @param replyLikeRecord
     * @return
     */
    @PostMapping("/getIsLikeByUserIdAndReplyId")
    public R getIsLikeByUserIdAndReplyId(@RequestBody ReplyLikeRecord replyLikeRecord) {
        ReplyLikeRecord record = replyLikeRecordService.getOne(
                new QueryWrapper<ReplyLikeRecord>().
                        eq("userId", replyLikeRecord.getUserid()).
                        eq("replyId", replyLikeRecord.getReplyid()).
                        eq("isLike", 1)
        );
        boolean isLike = false;
        if (record != null) {
            isLike = true;
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("isLike", isLike);
        return R.ok(map);
    }
}
